
import { useIpcRenderer } from "@vueuse/electron";
const ipcRenderer = useIpcRenderer();
export default {
    getItem(key) {
        return new Promise((resolve, reject) => {
            ipcRenderer.send("get-config")
            ipcRenderer.on("get-config", (event, data) => {
                // 检查data中是否有key
                if (data[key]) {
                    resolve(data[key])
                } else {
                    reject(new Error("key不存在"))
                }
            })
        })
    },
    getAllSetting() {
        return new Promise((resolve, reject) => {
            ipcRenderer.send("get-config")
            ipcRenderer.on("get-config", (event, data) => {
                resolve(data)
            })
        })
    },
    changeConfig(value) {
        return new Promise((resolve, reject) => {
            ipcRenderer.send("set-config", value)
            localStorage.setItem("config", JSON.stringify(value))
            resolve(JSON.parse(localStorage.getItem("config")))
        })
    },
    changeSettingItem(key, value) {
        return new Promise((resolve, reject) => {
            ipcRenderer.send("set-config-item", {
                key: key,
                value: value
            })
            let config = JSON.parse(localStorage.getItem("config"))
            config.setting[key] = value
            localStorage.setItem("config", JSON.stringify(config))
            resolve(config)
        })
    },

    changeSetting(value) {
        const func = (event, data) => {
            data.setting = JSON.parse(value)
            console.log(data);
            ipcRenderer.send("set-config", data)
            ipcRenderer.removeListener("get-config", func)
            localStorage.setItem("config", JSON.stringify(data))
        }
        return new Promise((resolve, reject) => {
            ipcRenderer.on("get-config", func)
            ipcRenderer.send("get-config", value)
        })




    }

}

